package com.microblogforsim.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.microblogforsim.R;
import com.microblogforsim.model.HotWord;

import java.util.ArrayList;

public class HotWordAdapter extends RecyclerView.Adapter<HotWordAdapter.BaseViewHolder> {
    private ArrayList<HotWord> hotWords;
    private OnRecyItemClickListener onRecyItemClickListener;
    private OnRecyItemLongClickListener onRecyItemLongClickListener;
    public interface OnRecyItemClickListener{
        void onClick(View view, int position);
    }
    public interface OnRecyItemLongClickListener{
        void onClick(View view, int position);
    }
    public void setOnRecyItemClickListener(OnRecyItemClickListener onRecyItemClickListener) {
        this.onRecyItemClickListener = onRecyItemClickListener;
    }
    public void setOnRecyItemLongClickListener(OnRecyItemLongClickListener onRecyItemLongClickListener) {
        this.onRecyItemLongClickListener = onRecyItemLongClickListener;
    }

    public void replaceAll(ArrayList<HotWord> data){
        hotWords = new ArrayList<>();
        hotWords.clear();
        if (data != null && data.size() > 0){
            hotWords.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.find_list_item,parent,false));
    }
    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, int position) {
        holder.setData(hotWords.get(position),position);
        if (onRecyItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = holder.getLayoutPosition();
                    onRecyItemClickListener.onClick(holder.itemView,i);
                }
            });
        }
        if (onRecyItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int i = holder.getLayoutPosition();
                    onRecyItemLongClickListener.onClick(holder.itemView,i);
                    return true;
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return hotWords.size();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder{
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }
        void setData(HotWord word, int position){

        }
    }

    private class OneViewHolder extends BaseViewHolder{
        private TextView tv_word,tv_type;
        private ImageView iv_more;
        public OneViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_word = itemView.findViewById(R.id.find_list_item_word);
            tv_type = itemView.findViewById(R.id.find_list_item_type);
            iv_more = itemView.findViewById(R.id.find_list_item_more);
        }

        @SuppressLint("ResourceAsColor")
        @Override
        void setData(HotWord word, int position) {
            if (word != null){
                tv_word.setText(word.getWord());
                switch (word.getType()){
                    case 1:
                        tv_type.setText("荐");
                        tv_type.setBackgroundResource(R.drawable.bg_tuijian);
                        tv_word.setText(word.getWord());
                        iv_more.setVisibility(View.GONE);
                        break;
                    case 2:
                        tv_type.setText("新");
                        tv_type.setBackgroundResource(R.drawable.bg_new);
                        tv_word.setText(word.getWord());
                        iv_more.setVisibility(View.GONE);
                        break;
                    case 3:
                        tv_word.setText(word.getWord());
                        tv_type.setVisibility(View.GONE);
                        iv_more.setVisibility(View.GONE);
                        break;
                    default:
                        tv_type.setVisibility(View.GONE);
                        tv_word.setText(word.getWord());
                        tv_word.setTextColor(Color.parseColor("#FF8200"));
                        tv_word.setTextSize(18);
                        iv_more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

}
